package com.css.dzj.dzjzhfx.modules.object;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.dzj.dzjzhfx.common.util.HttpClientUtil;
import com.css.dzj.dzjzhfx.common.util.R;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/object")
@PropertySource(value = "classpath:config.properties")
public class GetObjectByYear {

    @Value("${apURL}")
    private String apURL;
    @GetMapping("/getmapyear")
    public R getMap(String year){
        /**
         * 获取建设单位
         */
        String[] mapPro=new String[]{"南海诸岛","北京","天津","上海","重庆", "河北","河南","云南", "辽宁","黑龙江", "湖南", "安徽","山东","新疆","江苏", "浙江","江西","湖北","广西","甘肃","山西","内蒙古","陕西","吉林","福建","贵州","广东","青海","西藏","四川","宁夏","海南","台湾","香港","澳门"};
        String msgCon= HttpClientUtil.doGet(apURL+"/constr/countconstryear?year="+year);
        JSONObject jsonCon= JSON.parseObject(msgCon);
        Map<String, Object> mapCon = new HashMap<>();
        if(!StringUtils.isEmpty(msgCon)){
            Object constr=jsonCon.get("count");
            mapCon= JSONObject.parseObject(constr.toString());
        }

        /**
         * 获取安评单位
         */
        String msgUnit= HttpClientUtil.doGet(apURL+"/evalunit/countunityear?year="+year);
        JSONObject jsonUnit= JSON.parseObject(msgUnit);
        Map<String, Object> mapUnit = new HashMap<>();
        if(!StringUtils.isEmpty(msgUnit)){
            Object unit=jsonUnit.get("count");
            mapUnit= JSONObject.parseObject(unit.toString());

        }
        List<Map<String,Object>> list=new ArrayList();
        list.add(mapCon);
        list.add(mapUnit);
        Map<String,Object> mapAll=new HashMap<>();
        for(Map<String,Object> map:list){
            for(Map.Entry<String,Object> entry:map.entrySet()){
                String key=entry.getKey();
                Integer value=(Integer) entry.getValue();
                Integer valueAll=(Integer) mapAll.get(entry.getKey());
                if(valueAll==null){
                    mapAll.put(key,value);
                }else{
                    valueAll=valueAll.intValue()+value.intValue();
                    mapAll.put(key,valueAll);
                }
            }
        }
        List<Map<String,Object>> listNew=new ArrayList();
        Integer num=0;
        for (Map.Entry<String,Object> entry:mapAll.entrySet()) {
            String name=entry.getKey();
            String newName=name.replace("市","");
            Integer value=(Integer)entry.getValue();
            num+=value;
            HashMap map=new HashMap();
            map.put("name",newName);
            map.put("value",value);
            listNew.add(map);

        }
        for(String arr:mapPro){
            HashMap mapNew=new HashMap();
            for (Map map:listNew) {
               String str=(String)map.get("name");
               if(!arr.equals(str)){
                   mapNew.put("name",arr);
                   mapNew.put("value",0);
               }
            }
            listNew.add(mapNew);
        }
        String dataUnit=JSON.toJSONString(listNew);
        return R.ok().put("dataUnit",dataUnit).put("cityTotal",num);
    }

    /***
     * 获取饼状图
     * @return
     */
    @GetMapping("/getpieyear")
    public R getPie(String year){
        /**
         * 获取建设单位
         */
        String msgCon= HttpClientUtil.doGet(apURL+"/constr/countconstryear?year="+year);
        JSONObject jsonCon= JSON.parseObject(msgCon);
        Map<String, Object> mapCon = new HashMap<>();
        if(!StringUtils.isEmpty(msgCon)){
            Object constr=jsonCon.get("count");
            mapCon= JSONObject.parseObject(constr.toString());
        }
        List listContr=new ArrayList();
        for (Map.Entry<String,Object> entry:mapCon.entrySet()) {
            HashMap map=new HashMap();
            String name=entry.getKey();
            Integer value=(Integer)entry.getValue();
            map.put("name",name);
            map.put("value",value);
            listContr.add(map);
        }
        String dataConstr=JSON.toJSONString(listContr);
        /**
         * 获取安评单位
         */
        String msgUnit= HttpClientUtil.doGet(apURL+"/evalunit/countunityear?year="+year);
        JSONObject jsonUnit= JSON.parseObject(msgUnit);
        Map<String, Object> mapUnit = new HashMap<>();
        if(!StringUtils.isEmpty(msgUnit)){
            Object unit=jsonUnit.get("count");
            mapUnit= JSONObject.parseObject(unit.toString());
        }

        List listUnit=new ArrayList();
        for (Map.Entry<String,Object> entry:mapUnit.entrySet()) {
            HashMap map=new HashMap();
            String name=entry.getKey();
            Integer value=(Integer)entry.getValue();
            map.put("name",name);
            map.put("value",value);
            listUnit.add(map);
        }
        String dataUnit=JSON.toJSONString(listUnit);
        return R.ok().put("dataUnit",dataUnit).put("dataConstr",dataConstr);
    }

    /***
     * 获取柱状图
     * @return
     */
    @GetMapping("/getbaryear")
    public R getBar(String year){
        /**
         * 获取建设单位
         */
        String msgCon= HttpClientUtil.doGet(apURL+"/constr/countconstryear?year="+year);
        JSONObject jsonCon= JSON.parseObject(msgCon);
        Map<String, Object> mapCon = new HashMap<>();
        if(!StringUtils.isEmpty(msgCon)){
            Object constr=jsonCon.get("count");
            mapCon= JSONObject.parseObject(constr.toString());
        }
        List listContr=new ArrayList();

        /**
         * 获取安评单位
         */
        String msgUnit= HttpClientUtil.doGet(apURL+"/evalunit/countunityear?year="+year);
        JSONObject jsonUnit= JSON.parseObject(msgUnit);
        Map<String, Object> mapUnit = new HashMap<>();
        if(!StringUtils.isEmpty(msgUnit)){
            Object unit=jsonUnit.get("count");
            mapUnit= JSONObject.parseObject(unit.toString());
        }

        List listUnit=new ArrayList();
        /**
         * 获取建设单位地址
         */
        List<String> listCon=new ArrayList<>();
        for (Map.Entry<String,Object> entry:mapCon.entrySet()) {
            String name=entry.getKey();
            listCon.add(name);
        }
        /***
         * 获取安评单位地址
         */
        List<String> listUni=new ArrayList<>();
        for (Map.Entry<String,Object> entry:mapUnit.entrySet()) {
            String name=entry.getKey();
            listUni.add(name);
        }
        for (String con:listCon) {
            if(!listUni.contains(con)){
                mapUnit.put(con,0);
            }
        }
        for (String uni:listUni){
            if (!listCon.contains(uni)){
                mapCon.put(uni,0);
            }
        }
        for (Map.Entry<String,Object> entry:mapCon.entrySet()) {
            HashMap map=new HashMap();
            String name=entry.getKey();
            Integer value=(Integer)entry.getValue();
            map.put("name",name);
            map.put("value",value);
            listContr.add(map);
        }
        String dataConstr=JSON.toJSONString(listContr);

        for (Map.Entry<String,Object> entry:mapUnit.entrySet()) {
            HashMap map=new HashMap();
            String name=entry.getKey();
            Integer value=(Integer)entry.getValue();
            map.put("name",name);
            map.put("value",value);
            listUnit.add(map);
        }
        String dataUnit=JSON.toJSONString(listUnit);
        return R.ok().put("dataUnit",dataUnit).put("dataConstr",dataConstr);
    }

}
